package com.kawaiim.zhj.OutCard;

import java.util.ArrayList;
import java.util.List;

import com.kawaiim.zhj.entity.Card;
import com.kawaiim.zhj.entity.Player;
import com.kawaiim.zhj.Operator.PeopleOperator;
import com.kawaiim.zhj.utilities.Constants;
public class ComputerRemindCard {

    //电脑想出什么牌就出什么牌
    private static List<Card> Robort_Free_Remind(List<Card> deck) {
        //找同类型自己能出的最小的牌（不能随便拆）
        Player player = new Player();
        player.cards = deck;
        player.htStyle = OutCardStyleEnum.CANT_OUT;
        //利用玩家的提示
        PlayerRemindCard.peopleFirstHint(player, false);
        List<Card> resCards = player.htCards;

        //因为playerRemind提示后htStyle会到下一个，所以按照下一个处理
        switch (player.htStyle) {

            case TWO:
            case ONE:
                //不出3个2，不带对2，//除非能直接出完或者出完这些牌可以一次性出完其他牌，否则不允许出
                if (!afterThisOut_CanOutAllCard(deck, resCards)) {
                    resCards.clear();
                    //必须出牌，肯定还剩牌，找对子和单支
                    player.htStyle = OutCardStyleEnum.TWO;
                    PlayerRemindCard.peopleFirstHint(player, false);
                    resCards = player.htCards;
                }
                break;
        }

        return resCards;
    }

    public static List<Card> AIOutCard(List<Card>[] peoplesCard,int whoOut, int nowPerson, List<Card> prevCard) {
        //nowPerson：谁要出牌     whoOut：上家出牌的是谁
        List<Card> curOutCards = new ArrayList<>() ;
        OutCardStyle preOutCardStyle = OutCardStyle.judgeCardStyle(prevCard);
        //如果想出什么牌就出什么牌
        if (preOutCardStyle.outCardStyleEnum == OutCardStyleEnum.CANT_OUT) {
            return Robort_Free_Remind(peoplesCard[nowPerson]);
        }
        //如果是一家
        if (null != Constants.partner.get(whoOut) && Constants.partner.get(whoOut) == nowPerson && peoplesCard[nowPerson].size() > 3) {
             return curOutCards;
        }
        //如果敌方
        return CrushPreCard.crushPreCard(peoplesCard[nowPerson], preOutCardStyle, true, false);

    }



    //让后面的农民跑
    private static List<Card> letFarmerGo(List<Card>[] peoplesCard, int nowPerson,List<Card> preCard) {
               List<Card> curOutCards;
                Player player = new Player();
                player.cards = peoplesCard[nowPerson];
                player.htStyle =OutCardStyle.judgeCardStyle(preCard).outCardStyleEnum;
                PlayerRemindCard.peopleFirstHint(player, true);
                curOutCards = player.htCards;
                return curOutCards;
    }

    //todo 农民自由出牌
    private static List<Card> farmerFreeOutCard(List<Card>[] people, int nowPerson) {
        return Robort_Free_Remind(people[nowPerson]);
    }



    //判断出完这次牌后，下一次能不能一次出完
    private static boolean afterThisOut_CanOutAllCard(List<Card> people, List<Card> thisCard) {
        //如果当前已经可以打完了，返回true
        if (people.size() == thisCard.size()) {
            return true;
        }
        //保存当前的卡组
        List<Card> tDeck = Card.CopyListCard(people);
        //一出当前要出的牌
        for (int i = 0; i < thisCard.size(); i++) {
            tDeck.remove(thisCard.get(i));
        }

        //更新卡组的数量
        PeopleOperator.updateCardNum(tDeck);

        //判断下一次出牌
        //找同类型自己能出的最小的牌（不能随便拆）
        Player p = new Player();
        p.cards = tDeck;
        p.htStyle = OutCardStyleEnum.CANT_OUT;
        //利用玩家的提示
        PlayerRemindCard.peopleFirstHint(p, false);

        //如果该玩家提示的牌可全部出完
        return p.htCards.size() == tDeck.size();
    }
}
